﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;

namespace NJL.Web.Shop
{
    public partial class Default : BasePageVisitor
    {
        NJL.BLL.NJL_CS_SERVICE_TYPE bllServiceType = new BLL.NJL_CS_SERVICE_TYPE();
        NJL.BLL.NJL_COMPANY_SERVICE bllService = new BLL.NJL_COMPANY_SERVICE();
        NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
        public NJL.Model.NJL_COMPANY modelCompany;
        NJL.Model.NJL_USER modelUser = new Model.NJL_USER();
        NJL.BLL.NJL_USER bllUser = new BLL.NJL_USER();
        NJL.BLL.NJL_PERSON bllPerson = new BLL.NJL_PERSON();
        NJL.BLL.NJL_LEAVE_MESSAGE bllMessage = new BLL.NJL_LEAVE_MESSAGE();
        NJL.Model.NJL_LEAVE_MESSAGE modelMessage = new Model.NJL_LEAVE_MESSAGE();
        NJL.BLL.NJL_ATTRACTION bllAttraction = new BLL.NJL_ATTRACTION();

        protected DataSet dsService;

        protected void Page_Load(object sender, EventArgs e)
        {
            modelCompany = bllCompany.GetModel(Request.QueryString["id"]);
            if (!IsPostBack)
            {
                bind();
            }
        }
        void bind()
        {
            if (Request.QueryString["id"] == null || Request.QueryString["id"] == "")
            {
                Response.Redirect("/", true);
            }
            modelCompany = bllCompany.GetModel(Request.QueryString["id"]);
            //DataSet dsService;
            if (modelCompany != null)
            {  
                #region 服务列表
                if (Request.QueryString["typeid"] != null)
                {
                    dsService = bllService.GetList("NJL_COMPANY_ID='" + modelCompany.ID + "' and NJL_COMPANY_SERVICE.TYPE_CODE='" + Request.QueryString["typeid"] + "'");
                    if (dsService.Tables[0].Rows.Count > 8)
                    {
                        pager.InnerHtml = Royalsoft.Common.Pager.GetPageNum(dsService, rptServiceList, 8, new string[] { "id" }, "/shangpu/type-"+Request.QueryString["typeid"]+"-");
                    }
                    else
                    {
                        rptServiceList.DataSource = dsService;
                        rptServiceList.DataBind();
                    }
                }
                else
                {
                   dsService = bllService.GetList("NJL_COMPANY_ID='" + modelCompany.ID + "'");
                   if (dsService.Tables[0].Rows.Count > 0)
                   {
                       if (dsService.Tables[0].Rows.Count > 8)
                       {
                           pager.InnerHtml = Royalsoft.Common.Pager.GetPageNum(dsService, rptServiceList, 8, new string[] { "id" }, "/shangpu/");
                       }
                       else
                       {
                           rptServiceList.DataSource = dsService;
                           rptServiceList.DataBind();
                       }
                   }
                   else
                   {
                       ltlDesc.Text = modelCompany.COMPANY_DESCRIPTION;
                   }

                }
                #endregion
                #region 服务类型
                dsService = bllService.GetCompanyIdByTypeName(modelCompany.ID);
                rptService.DataSource = dsService;
                rptService.DataBind();
                #endregion
                #region 活动列表
                NJL.BLL.NJL_ACTIVE bllActive = new BLL.NJL_ACTIVE();
                dsService = bllActive.GetList(10, "IS_PUBLISH=1 and NJL_COMPANY_ID='" + Request.QueryString["ID"] + "'", "PUBLISH_DATE_TIME desc");

                if (dsService != null && dsService.Tables.Count > 0 && dsService.Tables[0].Rows.Count > 0)
                {
                    divLastActive.Style["display"] = "block";

                    //if (dsService.Tables[0].Rows.Count == 1)
                    //{
                    //    DataRow drTemp = dsService.Tables[0].NewRow();
                    //    for (int i = 0; i < dsService.Tables[0].Columns.Count; i++)
                    //    {
                    //        drTemp[i] = dsService.Tables[0].Rows[0][i];
                    //    }
                    //    dsService.Tables[0].Rows.Add(drTemp);
                    //}
                }
                else
                {
                    divLastActive.Style["display"] = "none";
                }

                rptActive.DataSource = dsService;
                rptActive.DataBind();

                //int serviceCount = dsService.Tables[0].Rows.Count;
                //if (serviceCount <= 6)
                //{
                //    dsService = bllActive.GetList(2, "IS_PUBLISH=1 and NJL_COMPANY_ID='" + Request.QueryString["ID"] + "'", "PUBLISH_DATE_TIME desc");
                //}
                //else if (serviceCount <= 12 && serviceCount > 6)
                //{
                //    dsService = bllActive.GetList(5, "IS_PUBLISH=1 and NJL_COMPANY_ID='" + Request.QueryString["ID"] + "'", "PUBLISH_DATE_TIME desc");
                //}
                //else if (serviceCount > 12 && serviceCount <= 18)
                //{
                //    dsService = bllActive.GetList(9, "IS_PUBLISH=1 and NJL_COMPANY_ID='" + Request.QueryString["ID"] + "'", "PUBLISH_DATE_TIME desc");
                //}
                //else if (serviceCount > 18)
                //{
                //    dsService = bllActive.GetList(13, "IS_PUBLISH=1 and NJL_COMPANY_ID='" + Request.QueryString["ID"] + "'", "PUBLISH_DATE_TIME desc");
                //}
                #endregion
                #region 游客分享
                NJL.BLL.NJL_NEWS bllNews = new BLL.NJL_NEWS();
                DataSet ds = bllNews.GetList(4, "SHARE_COMPANY_ID='" + Request.QueryString["id"] + "'", "CREATE_DATE_TIME desc");
                rptShare.DataSource = ds;
                rptShare.DataBind();
                #endregion
                #region 点击点击次数
                if (bllCompany.updateHitandDate(Request.QueryString["id"]))
                {
                }
                #endregion
                LoadPageSEO();
            }
            else
            {
                Response.Redirect("/", true);
            }
        }
        /// <summary>
        /// 加载页面SEO
        /// </summary>
        private void LoadPageSEO()
        {
            if (Request.QueryString["typeid"] != null)
            {
                Model.NJL_CS_SERVICE_TYPE modelServiceType = bllServiceType.GetModel(Request.QueryString["typeid"].Trim());
                Page.Title = modelCompany.COMPANY_NAME + "" + modelServiceType.SEO_TITLE;//长尾词
                HtmlMeta myMeta = new HtmlMeta();
                myMeta.Name = "Keywords";
                myMeta.Content = modelCompany.COMPANY_NAME + "" + modelServiceType.SEO_KEYWORDS.Replace(",", "," + modelCompany.COMPANY_NAME);//关键词
                this.Header.Controls.Add(myMeta);
                HtmlMeta myMeta1 = new HtmlMeta();
                myMeta1.Name = "Description";
                myMeta1.Content = modelCompany.SEO_DESCRIPTION;
                this.Header.Controls.Add(myMeta1);
            }
            else
            {
                Page.Title = modelCompany.SEO_TITLE;
                HtmlMeta myMeta = new HtmlMeta();
                myMeta.Name = "Keywords";
                myMeta.Content = modelCompany.SEO_KEYWORDS;
                this.Header.Controls.Add(myMeta);
                HtmlMeta myMeta1 = new HtmlMeta();
                myMeta1.Name = "Description";
                myMeta1.Content = modelCompany.SEO_DESCRIPTION;
                this.Header.Controls.Add(myMeta1);
            }
        }

        protected void rptServiceList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Image img = e.Item.FindControl("imgPic") as Image;
                DataRowView row = (DataRowView)e.Item.DataItem;
                string id = row["IMAGE_CODE"].ToString();
                if (!string.IsNullOrEmpty(id))
                {
                    img.ImageUrl = BasePage.GetImageUrl(id, "6");
                }
                else
                {
                    img.ImageUrl = "~/images/nopic.jpg";
                }
            }
        }     
    }
}